//
//  NovelDatabase.m
//  UI-16数据库
//
//  Created by Xue Senhong on 15/5/11.
//  Copyright (c) 2015年 薛森鸿. All rights reserved.
//

#import "BrandDatabase.h"

@implementation BrandDatabase

+ (void)commitSql:(NSString *)str
{
    sqlite3 *db = [DB openDB];
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"成功");
    } else {
        NSLog(@"失败");
    }

}



+ (void)deleteWithbookId:(NSString *)book_id;
{
    NSString *str = [NSString stringWithFormat:@"delete from Collect where movieName='%@'", book_id];
    [BrandDatabase commitSql:str];
}


+ (NSMutableArray *)selectAll
{
    NSString *str = @"select Z_PK,ZFIRSTLETTER,ZIMAGE,ZNAME,ZID from ZBRAND";
    sqlite3 *db = [DB openDB];
    // 定义执行结果接受者
    sqlite3_stmt *stmt = nil;
    
    // &stmt查询的结果所在的位置, -1代表不限制长度
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    NSMutableArray *array = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            NSInteger zpk = sqlite3_column_int(stmt, 0);
            const unsigned char *zFirst = sqlite3_column_text(stmt, 1);
            const unsigned char *zImage = sqlite3_column_text(stmt, 2);
            const unsigned char *zName = sqlite3_column_text(stmt, 3);
            const unsigned char *zId = sqlite3_column_text(stmt, 4);
           

            NSString *first = [NSString stringWithUTF8String:(const char *)zFirst];
            NSMutableString *image = [NSMutableString stringWithUTF8String:(const char *)zImage];
            NSString *name = [NSString stringWithUTF8String:(const char *)zName];
            NSString *iD = [NSString stringWithUTF8String:(const char *)zId];
           
            ZBrandModel *model = [[ZBrandModel alloc] init];
            model.ZFIRSTLETTER = first;
            model.ZID = iD;
            model.ZIMAGE = image;
            model.ZNAME = name;
            model.Z_PK = zpk;
            

            
            [array addObject:model];
        }
    }
    return array;
}



+ (ZBrandModel *)selectModel:(NSString *)ZID
{
    NSString *str = [NSString stringWithFormat: @"select Z_PK,ZFIRSTLETTER,ZIMAGE,ZNAME,ZID from ZBRAND where ZID=%@ ", ZID];
    sqlite3 *db = [DB openDB];
    // 定义执行结果接受者
    sqlite3_stmt *stmt = nil;
    
    ZBrandModel *model = [[[ZBrandModel alloc] init] autorelease];

    // &stmt查询的结果所在的位置, -1代表不限制长度
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            NSInteger zpk = sqlite3_column_int(stmt, 0);
            const unsigned char *zFirst = sqlite3_column_text(stmt, 1);
            const unsigned char *zImage = sqlite3_column_text(stmt, 2);
            const unsigned char *zName = sqlite3_column_text(stmt, 3);
            const unsigned char *zId = sqlite3_column_text(stmt, 4);
            
            
            NSString *first = [NSString stringWithUTF8String:(const char *)zFirst];
            NSMutableString *image = [NSMutableString stringWithUTF8String:(const char *)zImage];
            NSString *name = [NSString stringWithUTF8String:(const char *)zName];
            NSString *iD = [NSString stringWithUTF8String:(const char *)zId];
            
            model.ZFIRSTLETTER = first;
            model.ZID = iD;
            model.ZIMAGE = image;
            model.ZNAME = name;
            model.Z_PK = zpk;
            
            
            
        }
    }
    return model;
}


















@end
